<?php 
////////////////////////////////////////////////////////////////////////////////
//   Copyright (C) ReloadCMS Development Team                                 //
//   http://reloadcms.sf.net                                                  //
//                                                                            //
//   This program is distributed in the hope that it will be useful,          //
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
//                                                                            //
//   This product released under GNU General Public License v2                //
////////////////////////////////////////////////////////////////////////////////
//
//        ReloadCMS multilangual pages module revision 0.3
//
/////////////////////////////////


$pages_path=DATA_PATH."pages";
if(!isset($_GET['locale']))
{
$user_locale=$system->config['language'];
}
else
{
$user_locale=$_GET['locale'];
}
if (isset($_GET['pageid']))
{
$page_req=$_GET['pageid'];
if (ereg ("[/<>\:\"';.]",$page_req)) $page_req='';
else $page_req=htmlspecialchars($page_req);
}

function exist_page($page_id,$page_locale)
{
global $pages_path;
	if (file_exists($pages_path.'/'.$page_id.'.'.$page_locale))
			{
				return true;
			}
			else 
			{
				return false;
			}
}

function load_page($page_id,$page_locale)
		{
		global $pages_path;

		if (exist_page($page_id,$page_locale))
		{
			$page_content=file_get_contents($pages_path.'/'.$page_id.'.'.$page_locale);
			show_window('',$page_content,'');
		}
		else 
		{
			show_window(__('Error'),__('Page with such id or locale is not exists'), 'center');
		}
		}

function show_addpage_form()
{
$frm='
<form method="post" action="" name="pageadd">'.
  __('Page name').':<input type="text" name=page_name> '.__('Lang').':   
  <select name="page_locale">
  <option>uk</option>
  <option>ru</option>
  <option>en</option>
  <option>cs</option>
  <option>de</option>
  <option>el</option>
  <option>sk</option>
  <option>es</option>
  <option>sv</option>
  <option>tr</option>
  </select>
<br>
       <textarea cols="70" rows="30" name="page_content"></textarea><br>
       <input type="submit">
       </form>
	';
show_window(__('Adding page'),$frm, 'center');
}

function showcode_page()
{
$pname=explode('.',$_GET['pageid']);
$outpt='
<table width="100%">
<tr class="row3">
<td>'.__('Navigation multilangual').':</td><td><textarea>?module=pages&pageid='.$pname[0].'</textarea></td>
</tr>
<tr class="row3">
<td>'.__('Navigation direct').':</td><td><textarea>?module=pages&pageid='.$pname[0].'&locale='.$pname[1].'</textarea></td>
</tr>
<tr class="row3">
<td>'.__('HTML multilangual').':</td><td><textarea><a href="?module=pages&pageid='.$pname[0].'">'.$pname[0].'</a></textarea></td>
</tr>
<tr class="row3">
<td>'.__('HTML direct').':</td><td><textarea><a href="?module=pages&pageid='.$pname[0].'&locale='.$pname[1].'">'.$pname[0].'</a></textarea></td>
</tr>

</table>
';
show_window(__('Pages navigation code'),$outpt,'left');
}

function show_pageslist()
{
$result='<table width="100%">';
global $pages_path;
$allpages=rcms_scandir($pages_path);
foreach($allpages as $eachpage)
	{
	$fname=explode('.',$eachpage);
	$result.='<tr class=row1><td><a href="?module=pages&pageadmin&pageid='.$eachpage.'&modify">'.$eachpage.'</a></td><td><a href="?module=pages&pageadmin&pageid='.$eachpage.'&delete">'.__('Delete').'</a></td><td><a href="?module=pages&pageid='.$fname[0].'&locale='.$fname[1].'">'.__('Preview').'</a></td><td><a href="?module=pages&pageadmin&pageid='.$eachpage.'&showcode">'.__('Code').'</a></td></tr>';
	}
$result.='</table>';
show_window(__('Pages list'),$result,'left');
}

function modify_page()
{
global $pages_path;
$fname=explode('.',$_GET['pageid']);
save_page();
$frm='
<form method="post" action="" name="pageadd">
  Page name:<input type="text" name="page_name" value="'.$fname[0].'"> locale:   
  <select name="page_locale">
  <option>uk</option>
  <option>ru</option>
  <option>en</option>
  <option>cs</option>
  <option>de</option>
  <option>el</option>
  <option>sk</option>
  <option>es</option>
  <option>sv</option>
  <option>tr</option>
  </select>
<br>
       <textarea cols="70" rows="30" name="page_content">'.file_get_contents($pages_path.'/'.$_GET['pageid']).'</textarea><br>
       <input type="submit">
       </form>
	';
show_window(__('Modify page'),$frm,'center');
}


function save_page()
{
global $pages_path;
if ((isset($_POST['page_name'])) AND(isset($_POST['page_locale'])) AND(isset($_POST['page_content'])))
{
$filename=$pages_path.'/'.$_POST['page_name'].'.'.$_POST['page_locale'];
$content=$_POST['page_content'];
$handle = fopen($filename,'w');
fwrite($handle, $content);
fclose($handle);
show_window(__('Result'),__('Page saved'),'center');
}
}

function delete_page()
{
global $pages_path;
if (isset($_GET['delete']))
{
$name=$pages_path.'/'.$_GET['pageid'];
unlink($name);
}
}
// lets adminka begins
if($system->checkForRight('ROOT')){
show_window('','<a href="?module=pages&pageadmin">'.__('Pages management').'</a>','center');
if (isset($_GET['pageadmin']))
{
if (isset($_GET['addnew']))
{
save_page();
show_addpage_form();
}
if (isset($_GET['delete']))
{
delete_page();
}
if (isset($_GET['modify']))
{
modify_page();
}
if (isset($_GET['showcode']))
{
showcode_page();
}
show_window('','<a href="?module=pages&pageadmin&addnew">'.__('Add new page').'</a>','left');
show_pageslist();
}
if (isset($page_req))
{
load_page($page_req,$user_locale);
}
} //end of adminka
else {
if (isset($page_req))
{
load_page($page_req,$user_locale);
}
else 
{
show_window(__('Error'),__('No page ID specified'),'center');
}
}

?>